package com.cz.mes.view;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ColumnGenFromFront {
    public static void main(String[] args) {
        String dbGroup = "uat3";
        String tableName = "缺料分析";
        String tableNameEN = "shortage_bom_detail";

        try {
            Entity table = Db.use(dbGroup).queryOne("select id from mes_vw_table where table_name = ?",tableName);
            String tableId = table.getStr("id");
            System.out.println("tableId:" + tableId);

            File viewFile = new File("C:\\Users\\root\\Desktop\\viewFromFront.txt");
            JSONArray jsonArray =JSONUtil.readJSONArray(viewFile, StandardCharsets.UTF_8);
            for (int i = 0; i < jsonArray.size(); i++) {
                String dataIndex = jsonArray.getStr(i);

                Map<String,Object> params = new HashMap<>();
                params.put("table_id", tableId);
                params.put("data_index", dataIndex);
                Entity entity = Db.use(dbGroup).queryOne("select * from mesdb.mes_vw_column where table_id = :table_id and data_index = :data_index and status = 1",params);
                if(entity == null) {
                    String sql = "SELECT TABLE_NAME,COLUMN_NAME,COLUMN_TYPE,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME=:column_name AND TABLE_SCHEMA= :table_schema " +
                            " AND TABLE_NAME=:table_name";
                    Map<String,Object> params2 = new HashMap<>();
                    params2.put("column_name", StrUtil.toUnderlineCase(dataIndex));
                    params2.put("table_schema", "mesdb");
                    params2.put("table_name", tableNameEN);

                    Entity column = Db.use(dbGroup).queryOne(sql, params2);
                    if(column != null) {
                        String dataType = column.getStr("COLUMN_TYPE");
                        String columnComment = column.getStr("COLUMN_COMMENT");
                        String realTableName = column.getStr("TABLE_NAME");
                        String columnName = column.getStr("COLUMN_NAME");

                        int columnType = -1;
                        if (StrUtil.contains(dataType, "varchar") || StrUtil.contains(dataType, "char")) {
                            columnType = 0;
                        }
                        if (StrUtil.contains(dataType, "datetime")) {
                            columnType = 2;
                        }
                        if (StrUtil.contains(dataType, "decimal")) {
                            columnType = 5;
                        }
                        if (StrUtil.contains(dataType, "int")) {
                            columnType = 4;
                        }

                        System.out.println(tableName + " " + dataType + " " + columnComment + " " + realTableName + " " + columnName + " " + columnType);

                        Entity vwColumn = new Entity("mes_vw_column");
                        vwColumn.set("table_id", tableId);
                        vwColumn.set("column_title", columnComment);
                        vwColumn.set("data_index", dataIndex);
                        vwColumn.set("db_table_name", realTableName);
                        vwColumn.set("db_column_name", columnName);
                        vwColumn.set("width", 0);
                        vwColumn.set("column_type", columnType);
                        vwColumn.set("is_required",0);
                        vwColumn.set("application_flag",0);
                        vwColumn.set("status",1);
                        vwColumn.set("create_by", 1);
                        vwColumn.set("create_on", new Date());

                        Db.use(dbGroup).insert(vwColumn);
                    }else {
                        System.out.println("column not found");
                    }

                }



            }
        } catch (SQLException e) {
            e.printStackTrace();
        }




    }

}
